package com.admin.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.common.domain.dto.LoginUserDto;
import com.common.domain.entity.Department;
import com.common.domain.entity.LoginUser;
import com.common.domain.entity.ResponseResult;
import com.common.domain.entity.User;
import com.common.domain.vo.AdminUserInfoVo;
import com.common.domain.vo.MenuVo;
import com.common.domain.vo.RoutersVo;
import com.common.domain.vo.UserInfoVo;
import com.common.enums.AppHttpCodeEnum;
import com.common.service.AdminService;
import com.common.service.MenuService;
import com.common.service.RoleService;
import com.common.service.UserService;
import com.common.util.BeanCopyUtils;
import com.common.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private AdminService adminService;


    @Autowired
    private MenuService menuService;

    @Autowired
    private RoleService roleService;
    @GetMapping("/validUsername")
    public ResponseResult validUsername(String userName, String type){
        return userService.validUsername(userName, type);
    }

    @GetMapping("/authCode")
    public void getAuthCode(HttpServletRequest request, HttpServletResponse response) throws IOException, IOException {
        // 定义图形验证码的长和宽
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100, 4, 100);
        // 图形验证码写出，可以写出到文件，也可以写出到流
        lineCaptcha.write(response.getOutputStream());
        //将验证码存入session
        String code = lineCaptcha.getCode();
        request.getSession().setAttribute("authCode",code);
        // 关闭流
        response.getOutputStream().close();
    }

    @PostMapping("/login")
    public ResponseResult login(@RequestBody LoginUserDto loginUserDto,HttpServletRequest request) {
        //校验验证码
        String authCode = (String)request.getSession().getAttribute("authCode");

        if(!loginUserDto.getAuthCode().equalsIgnoreCase(authCode)){
            return ResponseResult.errorResult(AppHttpCodeEnum.VALID_CODE_ERROR);
        }

        User user = BeanCopyUtils.copyBean(loginUserDto, User.class);
        return adminService.login(user);
    }

    @PostMapping("/logout")
    public ResponseResult logout(){
        return adminService.logout();
    }

    @GetMapping("getInfo")
    public ResponseResult getInfo(){
        //获取当前登录的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        //得到用户id后查询用户的权限信息
        List<String> perms =  menuService.selectPermsByUserId(loginUser.getUser().getId());

        //查询用户的角色权限
        List<String> roleKeyList =  roleService.selectRoleKeyList(loginUser.getUser().getId());

        //获取用户信息
        User user = loginUser.getUser();
        UserInfoVo userInfoVo = BeanCopyUtils.copyBean(user, UserInfoVo.class);
        AdminUserInfoVo adminUserInfoVo = new AdminUserInfoVo(perms, roleKeyList ,userInfoVo);

        return ResponseResult.okResult(adminUserInfoVo);
    }


    @GetMapping("/getRouters")
    public ResponseResult<RoutersVo> getRouters(){
        //获取当前登录的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();

        List<MenuVo> menus = menuService.selectRouterMenuTreeByUserId(loginUser.getUser().getId());

        return ResponseResult.okResult(new RoutersVo(menus));
    }


    @GetMapping("/list")
    public ResponseResult allUserList(Integer pageNum, Integer pageSize, String name,String className, String studentId){
        return userService.allUserList(pageNum,pageSize,name,className,studentId);
    }

    @DeleteMapping("/deleteUser/{ids}")
    public ResponseResult deleteUser(@PathVariable String ids){
        return userService.deleteUser(ids);
    }

    @PostMapping("/insertUser")
    public ResponseResult insertUser(@RequestBody User user){
        return userService.insertUser(user);
    }

    @PutMapping("/updateUser")
    public ResponseResult updateUser(@RequestBody User user){
        return userService.updateUser(user);
    }

    @GetMapping("changeStatus")
    public ResponseResult changeStatus(Long id, String status){
        return userService.changeStatus(id, status);
    }

}
